package com.bonc.event.eventtype;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.NlpAnalysis;
import org.springframework.beans.factory.annotation.Autowired;

import com.bonc.text.entity.recognition.RecResult;
import com.bonc.text.service.impl.EntityRecognitionServiceImpl;
import com.bonc.utilities.EventUtility;

/**
 * @author donggui@bonc.com.cn
 * @version 2016 2016年7月1日 下午2:12:59
 */
public class ContextExtractor {

	@Autowired
	EntityRecognitionServiceImpl entityRecognitionService;

	Map<String, String> stopwords = EventUtility.loadStopwords();
	
	private int windowSize = 5;

	
	public ContextExtractor(){
	}	
	
	public String[] extract(String text, String keyword){
//		RecResult[] contexts = new RecResult[2*windowSize];
		String[] typeContexts = new String[2*windowSize];
		String[] posContexts = new String[2*windowSize];
		if(text!=null && !"".equals(text.trim()) && text.contains(keyword)){
//			List<RecResult> results = entityRecognitionService.getEntityList(text);			
			int idx = text.indexOf(keyword);
			String left="";
			String right="";
			if(idx >= 1){
				left = text.substring(0, idx);
			}
			if(idx <= text.length()-2){
				right = text.substring(idx+1,text.length()-1 );
			}
			
			//左特征
			if(left!=null && !"".equals(left.trim())){
				List<Term> leftTerms = seg(left);
				System.out.println("left=="+left);
//				List<RecResult> leftResults = entityRecognitionService.getEntityList(left);
				List<Term> leftList = new ArrayList<Term>();
				for(Term key: leftTerms){
//					int value = left.lastIndexOf(key.getWord());
					if(key.getNatureStr().contains("n")){
						leftList.add(key);	
						System.out.println("add=="+key.toString());
					}					
				}
//				//将map.entrySet()转换成list
//				List<Map.Entry<RecResult, Integer>> leftList =
//						new ArrayList<Map.Entry<RecResult, Integer>>(position.entrySet());
//				Collections.sort(leftList, new Comparator<Map.Entry<RecResult, Integer>>() {
//					//升序序排序
//					@Override
//					public int compare(Map.Entry<RecResult, Integer> o1, Map.Entry<RecResult, Integer> o2) {
//						return o1.getValue().compareTo(o2.getValue());
////						return o2.getValue().compareTo(o1.getValue());
//					}
//				});
				
				if(leftList!=null){
					for(int k=0;k< windowSize;k++){
						int i=windowSize-k-1;
						int j = leftList.size() - k -1;
						if(j>=0 && j <=  leftList.size() -1){
							typeContexts[i] = leftList.get(j).getNatureStr();
							System.out.println("leftType=="+typeContexts[i]);
						}
						
						if(j>=0 && j <= leftTerms.size() -1){
//							posContexts[i] = leftTerms.get(j).natrue().natureStr;
							posContexts[i] = leftTerms.get(j).toString();
							System.out.println("leftName=="+posContexts[i]);
						}
					}
				}
			}			
			
			//右特征
			if(right!=null && !"".equals(right.trim())){
				List<Term> rightTerms = seg(right);
				
//				List<RecResult> rightResults = entityRecognitionService.getEntityList(right);
//				Map<RecResult, Integer> rightPosition = new HashMap<RecResult, Integer>();
				List<Term> rightList = new ArrayList<Term>();
				for(Term key: rightList){
//					int value = right.lastIndexOf(key.getWord());
//					if(value>-1){
//						rightPosition.put(key, value);	
//					}
					if(key.getNatureStr().contains("n")){
						rightList.add(key);	
						System.out.println("addright=="+key.toString());
					}					
				}
				
//				List<Map.Entry<RecResult, Integer>> rightList =
//						new ArrayList<Map.Entry<RecResult, Integer>>(rightPosition.entrySet());
//				Collections.sort(rightList, new Comparator<Map.Entry<RecResult, Integer>>() {
//					//升序序排序
//					@Override
//					public int compare(Map.Entry<RecResult, Integer> o1, Map.Entry<RecResult, Integer> o2) {
//						return o1.getValue().compareTo(o2.getValue());
////						return o2.getValue().compareTo(o1.getValue());
//					}
//				});
				
				if(rightList!=null){
					for(int j=0;j < windowSize;j++){						
						int i = windowSize + j;
						
						if(j>=0 && j <=  rightList.size() -1){
//							typeContexts[i] = rightList.get(j).getNatureStr();
							typeContexts[i] = rightList.get(j).getNatureStr();
							System.out.println("rightType=="+typeContexts[i]);
						}
						
						if(j>=0 && j <=  rightTerms.size() -1){
//							posContexts[i] = rightTerms.get(j).natrue().natureStr;
							posContexts[i] = rightTerms.get(j).toString();
							System.out.println("rightNature=="+posContexts[i]);
						}
					}
				}
			}				
		}
//		return typeContexts;
		return getMergeArray(typeContexts,posContexts);
	}
	
	public List<Term> seg(String content){
		List<Term> results= new ArrayList<Term>();
		
		List<Term> terms = NlpAnalysis.parse(content.trim());
		String regex = "(?!^[\\d]+$)^[a-zA-Z0-9\u4E00-\u9FA5]+$";

		String trimWord = "";
		for (Term term : terms) {
			trimWord = term.getName().trim();
			if (trimWord!=null && !trimWord.isEmpty() && trimWord.length() > 1 
					&& !this.stopwords.containsKey(trimWord)
					&& trimWord.matches(regex)) {
				results.add(term);
				System.out.println(term.getName()+":"+term.getNatureStr());
			}
		}
		
		return results;
	}
	
	private String[] getMergeArray(String[] al,String[] bl) {
		String[] a = al;
		String[] b = bl;
		String[] c = new String[a.length + b.length];
		System.arraycopy(a, 0, c, 0, a.length);
		System.arraycopy(b, 0, c, a.length, b.length);
		return c;
	}
	
	private Term[] getMergeArray(Term[] al,Term[] bl) {
		Term[] a = al;
		Term[] b = bl;
		Term[] c = new Term[a.length + b.length];
		System.arraycopy(a, 0, c, 0, a.length);
		System.arraycopy(b, 0, c, a.length, b.length);
		return c;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
